import { Component, OnInit, ViewChild } from '@angular/core';
import { STColumn, STComponent, STData, STPage, STRes } from '@delon/abc/st';
import { SFSchema } from '@delon/form';
import { ModalHelper, _HttpClient } from '@delon/theme';

import { SysPermissionEditComponent } from './edit/edit.component';

@Component({
  selector: 'app-sys-permission',
  templateUrl: './permission.component.html'
})
export class SysPermissionComponent implements OnInit {
  url = `api/permissions`;
  searchSchema: SFSchema = {
    properties: {
      keyword: {
        type: 'string',
        title: '',
        ui: {
          placeholder: '关键词'
        }
      }
    }
  };
  @ViewChild('st') private readonly st!: STComponent;
  columns: STColumn[] = [
    { title: 'ID', index: 'ID', className: 'text-center' },
    { title: '名称', index: 'name', className: 'text-center' },
    { title: '请求uri', index: 'uri', className: 'text-center' },
    { title: '请求方式', index: 'method', className: 'text-center' },
    { title: '所属组', index: 'group', className: 'text-center', default: '-' },
    {
      title: '操作',
      buttons: [
        {
          text: '编辑',
          icon: 'edit',
          type: 'static',
          modal: {
            component: SysPermissionEditComponent,
            params: (record: STData) => record,
            modalOptions: { nzMaskClosable: false }
          },
          click: 'reload'
        },
        { text: '删除', type: 'del', icon: 'delete', className: 'text-error', click: (item: any) => this.del(item.ID) }
      ]
    }
  ];

  res: STRes = {
    reName: {
      total: 'data.total',
      list: 'data.list'
    }
  };
  paginate: STPage = {
    showSize: true
  };

  constructor(private http: _HttpClient, private modal: ModalHelper) {}

  ngOnInit = (): void => {};

  /**
   * 新增
   */
  add(): void {
    this.modal.createStatic(SysPermissionEditComponent, { i: { id: 0 } }).subscribe(() => this.st.reload());
  }

  /**
   * 删除
   *
   * @param id 数据id
   */
  del(id: number): void {
    this.http.delete(`api/permission/${id}`).subscribe(() => {
      this.st.reload();
    });
  }
}
